<?php
/**
 * Geneone: Content Management Framework
 * Default Entry Page
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */
 
/* All your errors are belong to us */
error_reporting (E_ALL);

/* Load relevant config file */
require ("configs/servers.php");

if (GENEONE_MULTIPLE_SERVER AND isset($gene_servers[$_SERVER['HTTP_HOST']])) {
	require_once ("configs/".$gene_servers[$_SERVER['HTTP_HOST']].".php");
} else {
	require ("configs/default.php");
}

/* Apache Option Multiview */
$multiviewBase = $_SERVER['SCRIPT_NAME']."/";
if (!isset($_GET['s']) && substr($_SERVER['REQUEST_URI'], 0, strlen($multiviewBase)) == $multiviewBase) {
	$_GET['s'] = substr($_SERVER['REQUEST_URI'], strlen($multiviewBase));
	if ($_SERVER['QUERY_STRING']) {
		$_GET['s'] = substr($_GET['s'], 0, -strlen($_SERVER['QUERY_STRING'])-($_SERVER['QUERY_STRING'] ? 1 : 0));
	}
	$_GET['s'] = urldecode($_GET['s']);
}

/* Load required core libraries */
define ("IN_GENE", TRUE);
require ("loader.php");

/* Templating */
$tpl =& Gene::getTemplate(array("template_dir" => Gene::getSetting("templates_dir")));

ob_start();

/* Start up Auth */
$auth =& Gene::getAuth();

/* Get GeneUser */
$user =& Gene::getUser();

if ($auth->getAuth()) {
	$tpl->assign ("loggedin", true);
	$tpl->assign ("member", $user->getInfo());
} else {
	$tpl->assign ("loggedin", false);
}

// No Object specified?
if (!$s = Gene_Request::getString("s", GENE_REQUEST_GET)) {
	$s = Gene::getSetting("default_obj");
}

$tpl->assign("objectid", $s);

/* Namespaces */
$namespaces = Gene::getSetting("namespaces");
$obj = explode (":", $s);
if (count($obj) == 2) {
	if (!isset($namespaces[$obj[0]])) {
		$obj = array(Gene::getSetting("default_namespace"), $obj['1']);
	} else {
		$obj = array($namespaces[$obj[0]], $obj[1]);
	}
} else {
	$obj = array(Gene::getSetting("default_namespace"), $obj[0]);
}

/* Subpage or Action */
$i = explode("/", $obj[1]);
if (count($i) > 1) {
	$obj[1] = $i[0];
	unset($i[0]);
} else {
	$i = array();
}

/* Import Namespace Class */
require_once ("Gene/Namespace/".ucfirst(strtolower($obj['0'])).".php");
$nsclass = "Gene_Namespace_".$obj['0'];

if (!call_user_func(array($nsclass, "call"), $obj['1'], $i)) {
	// We *MUST* get a TRUE back from the namespace call otherwise we BSoD
	header ("HTTP/".$_SERVER['SERVER_PROTOCOL']." 404 Not Found");
	$tpl->assign("searchfor", $obj['1']);
	$tpl->assign("finalbody", $tpl->fetch("errors/invalid"));
}

// application/xhtml+xml causes too many problems at the moment
header("Content-Type: text/html");

$tpl->display ("standard");

ob_end_flush();

/* cleanup */
Gene_Session::shutdown();
$db =& Gene::getDatabase();
$db->disconnect();
?>